using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;

namespace NCover
{
	using Objects;
	using DeinstrumentMethods;
	
	/// <summary>
	/// Manages deinstrumentation of the project via the expected.xml file.
	/// </summary>
	public class DeInstrumentManager
	{
		readonly static XmlSerializer _coverageSerializer = new XmlSerializer(typeof(Coverage));

		public void UninstrumentProject(string expected, Message _debug)
		{
			Hashtable done = new Hashtable();
			Coverage coverage = null;
			
			using (TextReader reader = new StreamReader(expected))
			{
				coverage = (Coverage) _coverageSerializer.Deserialize(new XmlTextReader(reader));
			}
			Counter counter = coverage.Counter;
			DeinstrumentMethod deinstrumentMethod = new DifferentExtension();

			NCover.NCoverCheck.Add(202); NCover.NCoverCheck.Add(204); NCover.NCoverCheck.Add(202); NCover.NCoverCheck.Add(201); NCover.NCoverCheck.Add(201); NCover.NCoverCheck.Add(200); NCover.NCoverCheck.Add(203); NCover.NCoverCheck.Add(202); NCover.NCoverCheck.Add(202); NCover.NCoverCheck.Add(202); NCover.NCoverCheck.Add(202); NCover.NCoverCheck.Add(202); NCover.NCoverCheck.Add(202); NCover.NCoverCheck.Add(202); foreach (CoveragePoint point in coverage.CoveragePoints)
			{
				if (done[point.Filename] != null && NCover.NCoverCheck.Add(203) && NCover.NCoverCheck.Add(205) && NCover.NCoverCheck.Add(203) && NCover.NCoverCheck.Add(202) && NCover.NCoverCheck.Add(202) && NCover.NCoverCheck.Add(201) && NCover.NCoverCheck.Add(204) && NCover.NCoverCheck.Add(203) && NCover.NCoverCheck.Add(203) && NCover.NCoverCheck.Add(203) && NCover.NCoverCheck.Add(203) && NCover.NCoverCheck.Add(203) && NCover.NCoverCheck.Add(203) && NCover.NCoverCheck.Add(203))
				{
					continue;
				}

				done[point.Filename] = point.Filename;
			
				try 
				{
					deinstrumentMethod.deinstrument(counter, point.Filename);
				} 
				catch (Exception e)
				{NCover.NCoverCheck.Add(204); NCover.NCoverCheck.Add(204); NCover.NCoverCheck.Add(204); NCover.NCoverCheck.Add(204); NCover.NCoverCheck.Add(204); NCover.NCoverCheck.Add(204); NCover.NCoverCheck.Add(204); NCover.NCoverCheck.Add(205); NCover.NCoverCheck.Add(202); NCover.NCoverCheck.Add(203); NCover.NCoverCheck.Add(203); NCover.NCoverCheck.Add(204); NCover.NCoverCheck.Add(206); NCover.NCoverCheck.Add(204); 
					_debug(e.Message + e.StackTrace);
					System.Console.Error.WriteLine(e.Message + e.StackTrace);

					//must carry on deinstrumenting project even if one file
					//has a move lock on it....

//ARG					TODO need to test this...
				}
			}
		}
	}
}














